local ViewNode = {}

ViewNode.ENUM = {
    NODE = "node",
    TOUCHNODE = "touchnode",
    SPRITE = "sprite",                          -- 图片 加scale9参数是九宫格图片id
    LABEL = "label",
    SIMPLE_BTN = "simple_btn",                  -- 缩小样式的按钮
    NORMAL_BTN = "normal_btn",                  -- 普通样式的按钮
    CMN_BTN = "cmn_btn",                        -- 通用样式的按钮
    LIST = "list_old",                          -- 列表
    LIST_NEW = "list"                           -- 新版的列表控件         
}

ViewNode.REMOTE_TYPE = {
    PC = "pc",
    MOBILE = "mobile"
}

ViewNode.ANCHOR_POINTS = {
    ["center"] = Vector2(0.5, 0.5),
    ["left_top"] = Vector2(0, 1),
    ["top"] = Vector2(0.5, 1),
    ["right_top"] = Vector2(1, 1),
    ["left"] = Vector2(0, 0.5),
    ["right"] = Vector2(1, 0.5),
    ["left_bottom"] = Vector2(0, 0),
    ["right_bottom"] = Vector2(1, 0),
    ["bottom"] = Vector2(0.5, 0)
}

ViewNode.CMN_BTN = {
    -- ["level_1"] = ui.BTN_LEVEL_1,                           -- 特殊按钮
    -- ["level_2"] = ui.BTN_LEVEL_2,                           -- 低阶按钮
    -- ["level_2s"] = ui.BTN_LEVEL_2S,                         -- 二级按钮
    -- ["level_3"] = ui.BTN_LEVEL_3,                           -- 三级按钮
    -- ["level_3s"] = ui.BTN_LEVEL_3S,                         -- 三级按钮小

    -- ["sp_2"] = ui.BTN_SP_2,                                 -- 特殊二级按钮
    -- ["sp_3"] = ui.BTN_SP_3,                                 -- 特殊三级按钮
    -- ["sp_3s"] = ui.BTN_SP_3S,                               -- 特殊三级按钮小
    -- ["sp_se"] = ui.BTN_SP_SE,                               -- 特殊高级按钮
    -- ["sp_lw"] = ui.BTN_SP_LW                                -- 特殊低级按钮
}

ViewNode.LIST_DIR = {
    ["both"] = 0,
    ["vertical"] = 1,
    ["horizontal"] = 2
}

ViewNode.FLIP = {
    X = "x",
    Y = "y",
    XY = "xy"
}

ViewNode.ALIGN = {
    -- ["center"] = cc.ui.TEXT_ALIGN_CENTER,
    -- ["left"] = cc.ui.TEXT_ALIGN_LEFT,
    -- ["right"] = cc.ui.TEXT_ALIGN_RIGHT,
}

function ViewNode:renderChildren(children,parent)
    self:addViewByParam(parent,children)
end

function ViewNode:addViewByParam(parent,param)
    for k,v in ipairs(param) do
        self:preParam(v)
        
        local posx,posy,anchor = v.x or 0,v.y or 0,v.anchor or "left_bottom"
        local node = self:getNodeByParam(v)

        node:addTo(parent,v.z):pos(posx,posy):anchor(self.ANCHOR_POINTS[anchor])

        if v.children and #v.children > 0 then
            self:addViewByParam(node,v.children)
        end

        if v.id then
            node:getRectTransform().name = v.id
        end

        local cViewNode = node:getComponent(SgsUI.ViewNode)
        cViewNode:UpdateParam()
    end
end

function ViewNode:getNodeByParam(param)
    local type = param.type
    local node = nil

    if type == ViewNode.ENUM.SPRITE then
        node = self:getSprite(param)
    elseif type == ViewNode.ENUM.LABEL then
        node = self:getLabel(param)
    else
        node = UI.newNode()
    end

    return node
end

function ViewNode:getLabel(param)
    local ret = UI.newLabel()
    local labelRes = ret:getComponent(SgsUI.LabelRes)
    labelRes:InitRes()
    labelRes.text = param.text
    labelRes.fontSize = param.size
    labelRes.color = Color(param.color[1]/255,param.color[2]/255,param.color[3]/255)
    return ret
end

function ViewNode:getSprite(param)
    local ret = UI.newSprite()
    local spriteRes = ret:getComponent(SgsUI.SpriteRes)

    local sprite = SgsUI.Editor.UI.GetSprite("Plist/"..param.plist.."/"..param.res)

    spriteRes.sprite = sprite
    ret.real_sprite = param.res_real
    spriteRes:SetNativeSize()

    return ret
end

function ViewNode:preParam(param)
    if not param then
        return
    end

    if param.pos then
        param.x,param.y = param.pos[1],param.pos[2]
    end

    if param.align and type(param.align) == "table" then
        param.anchor,param.x,param.y = param.align[1],param.align[2],param.align[3]
    end

    if param.size and self.ENUM.LABEL ~= param.type then
        param.width,param.height = param.size[1],param.size[2]
    end

    if param.hide then
        param.visible = false
    end

    if type(param.res) == "table" then
        param.res = self:getFormat(_t_unpack(param.res))
    end
end

function ViewNode:getFormat(...)
    return string.format(...)
end

return ViewNode